package com.wubotao.multithreading;

import java.util.concurrent.locks.Lock;

/**
 * @说明 儿子类，只消费
 */
public class Consumer implements Runnable {
	BankCard bc = null;
	Lock lock = null;

	Consumer(BankCard bc, Lock lock) {
		this.bc = bc;
		this.lock = lock;
	}

	public void run() {
		try {
			while (true) {
				
				try {
					lock.lock();
					System.out.print("儿子要消费，现在余额：" + bc.getBalance() + "\t");
					bc.setBalance(bc.getBalance() - 2000);
					System.out.println("儿子消费2000元，现在余额：" + bc.getBalance());
				} finally  {
					lock.unlock();
				}
				
				Thread.sleep(1 * 1000);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
